/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upn.sigemfi.can.bc;

import edu.upn.sigemfi.can.be.Banco;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Programador
 */
public class BancoDelegate {

    private BancoFacadeLocal local;

    public BancoDelegate(){
        local=lookupBancoFacade();
    }

    public void crear(Banco banco) throws Exception{
        try {
            local.create(banco);
        } catch (Exception e) {
            local=lookupBancoFacade();
            throw new Exception("Crear Banco" + e.getMessage(), e);
        }
    }

    public void editar(Banco banco) throws Exception{
        try {
            local.edit(banco);
        } catch (Exception e) {
            local=lookupBancoFacade();
            throw new Exception("Editar Banco" + e.getMessage(), e);
        }
    }

    public void eliminar(Banco banco) throws Exception{
        try {
            local.remove(banco);
        } catch (Exception e) {
            local=lookupBancoFacade();
            throw new Exception("Eliminar Banco" + e.getMessage(), e);
        }
    }

    public Banco buscar(long codigo) throws Exception{
        try {
            return local.find(codigo);
        } catch (Exception e) {
            local=lookupBancoFacade();
            throw new Exception("Buscar Banco" + e.getMessage(), e);
        }
    }

    public List<Banco> listar() throws Exception {
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupBancoFacade();
            throw new Exception("Listar Banco. " + e.getMessage(), e);
        }
    }


    private BancoFacadeLocal lookupBancoFacade() {
        try {
            Context c = new InitialContext();
            return (BancoFacadeLocal) c.lookup("java:comp/env/BancoFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }



}
